<template>
  <div>
    <!-- 导航面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodslist" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="120px"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="120px"
        ></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="220px">
          <template slot-scope="scoped">
            {{ (scoped.row.add_time * 1000) | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="320px">
          <template slot-scope="scope">
            <!-- {{scope}} -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="editBtn(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="removeById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 编辑弹出框dialog -->
    <el-dialog
      title="商品编辑"
      :visible.sync="editVisible"
      width="50%"
      :before-close="editClose"
    >
      <el-form
        :model="editForm"
        :rules="editRules"
        ref="editRef"
        label-width="100px"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="商品分类" prop="goods_cat">
          <el-cascader
            v-model="editForm.goods_cat"
            :options="catelist"
            :props="cateProps"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      // 商品数据信息
      goodslist: [],
      // 总条数
      total: 0,
      editVisible: false,
      catelist: [], // 分类数据
      cateProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        expandTrigger: 'hover',
      },
      editForm: {
        goods_id: '',
        goods_name: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        goods_cat: [],
        pics: [],
        attrs: [],
      },
      editRules: {
        goods_name: [
          {
            required: true,
            message: '请输入商品名称',
            trigger: 'blur',
          },
        ],
        goods_price: [
          {
            required: true,
            message: '请输入商品价格',
            trigger: 'blur',
          },
        ],
        goods_number: [
          {
            required: true,
            message: '请输入商品数量',
            trigger: 'blur',
          },
        ],
        goods_weight: [
          {
            required: true,
            message: '请输入商品重量',
            trigger: 'blur',
          },
        ],
        goods_cat: [
          {
            required: true,
            message: '请选择商品',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    this.getGoodsList()
    this.getCateList()
  },
  methods: {
    async getGoodsList() {
      const { data } = await this.$http.get('goods', { params: this.queryInfo })
      console.log('List数据', data)
      if (data.meta.status !== 200) {
        this.$message.error('获取商品列表信息失败')
      }
      this.$message.success('获取商品列表信息成功')
      this.goodslist = data.data.goods
      this.total = data.data.total
    },
    async getCateList() {
      const { data: res } = await this.$http.get('categories')
      console.log('商品分类', res)
      if (res.meta.status !== 200) {
        this.$message.error('获取分类数据失败')
      }
      this.$message.success('获取分类信息成功')
      this.catelist = res.data
    },
    handleChange() {
      console.log(this.editForm.goods_cat)
      if (this.editForm.goods_cat.length !== 3) {
        // 判断级联选择器的内容只能选择第三级，否则置空
        this.editForm.goods_cat = []
      }
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    removeById(id) {
      console.log('id---', id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`goods/${id}`)
          console.log('222', res)
          if (res.meta.status === 200) {
            this.$message({
              type: 'success',
              message: '删除成功!',
            })
            this.getGoodsList()
          } else {
            this.$message({
              type: 'error',
              message: '删除失败!',
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    addGoods() {
      this.$router.push('/goods/add')
    },
    editBtn(dd) {
      console.log('bb-', dd)
      this.editForm = dd
      this.editVisible = true
    },
    editClose() {
      this.$refs.editForm.resetFields()
    },
    async editSubmit() {
      const { data: res } = await this.$http.put(
        `goods/${this.editForm.goods_id}`,
        this.editForm
      )
      console.log('提交', res)
      this.editVisible = false
      this.getGoodsList()
    },
  },
}
</script>

<style lang="less" scoped>
</style>
